package com.jy.rhin.support.interceptor.convertor;

import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.oasis_open.docs.wsn.b_2.PauseSubscription;
import org.oasis_open.docs.wsn.b_2.PauseSubscriptionResponse;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service("AuditInfoConverter4TerminologyDocumentPauseSubscribe")
public class AuditInfoConverter4TerminologyDocumentPauseSubscribe extends BaseAuditInfoConverter<PauseSubscription, PauseSubscriptionResponse> {

    public AuditInfoConverter4TerminologyDocumentPauseSubscribe() {
        super(EventActionCodeEnum.EXECUTE,
                ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH,
                EventTypeCodeEnum.DOCUMENT_PAUSE_SUBSCRIBE,
                true,
                ParticipantObjectIDTypeCodeEnum.URI,
                ParticipantObjectTypeCodeRoleEnum.SUBSCRIBER,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-subandpub-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(PauseSubscription pauseSubscription, PauseSubscriptionResponse pauseSubscriptionResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(PauseSubscription pauseSubscription, PauseSubscriptionResponse pauseSubscriptionResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(PauseSubscription pauseSubscription, PauseSubscriptionResponse pauseSubscriptionResponse) {
        return new AuditParticipantUserDesc("documents-subandpub-service", ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
    }

    @Override
    String getObjectId(PauseSubscription pauseSubscription, PauseSubscriptionResponse pauseSubscriptionResponse) {
        if (Objects.nonNull(pauseSubscription)) {
            return pauseSubscription.getSubscriptionId();
        }
        if (Objects.nonNull(pauseSubscriptionResponse)) {
            if (Objects.nonNull(pauseSubscriptionResponse.getFault())) {
                return pauseSubscriptionResponse.getFault();
            }
            if (Objects.nonNull(pauseSubscriptionResponse.getSuccessMessge())) {
                return pauseSubscriptionResponse.getSuccessMessge();
            }
        }
        return "";
    }
    @Override
	public boolean isMatch(String method, Object data) {
		return "DocumentSubAndPubPortImpl.documentPauseSubscribe".equals(method);
	}
}
